<template>
    <!-- 列表页面 -->
    <div class="tableSample" v-if="!showEdit && !showList">
        <div class="header">
            <div class="header-left">
                <p class="title">区/县合伙人
                </p>
            </div>
            <div class="header-right">
                <lin-search @query="onQueryChange" placeholder="请输入省份名称"/>
                <lin-search @query="onQueryChangeOne" placeholder="请输入城市名称" style="margin-left: 10px;"/>
                <lin-search @query="onQueryChangeTwo" placeholder="请输入区/县名称" style="margin-left: 10px;"/>
            </div>
        </div>
        <!-- 表格 -->
        <lin-table
                border
                :tableColumn="tableColumn"
                :tableData="tableData"
                :operate="operate"
                @handleEdit="handleEdit"
                @handleList="handleList"
                @handleDelete="handleDelete"
                v-loading="loading"></lin-table>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :background="true"
                    :page-size="pageCount"
                    :current-page="currentPage"
                    v-if="refreshPagination"
                    layout="total,prev, pager, next, jumper"
                    :total="total_nums"
            ></el-pagination>
        </div>
    </div>
    <!-- 修改页面 -->
    <county-edit v-else-if="showEdit" @editClose="editClose" :countyId="countyId" ></county-edit>
    <!-- 修改页面 -->
    <county-edit-one v-else-if="showList" @listClose="listClose" :countyId="countyId" ></county-edit-one>
</template>

<script>
import partner from '@/models/partner'
import LinSearch from '@/components/base/search/lin-search'
import LinTable from '@/components/base/table/lin-table'
import CountyEdit from './CountyEdit'
import CountyEditOne from './CountyEditOne'

export default {
  components: {
    LinSearch,
    LinTable,
    CountyEdit,
    CountyEditOne,
  },
  data() {
    return {
      tableColumn: [
        { prop: 'province', label: '省份' },
        { prop: 'city', label: '城市' },
        { prop: 'county', label: '代理区/县' },
        { prop: 'name', label: '姓名' },
        { prop: 'phone', label: '电话' },
        { prop: 'id_card', label: '身份证号' },
        { prop: 'status_name', label: '状态' },
        { prop: 'invite_money', label: '返佣' },
        { prop: 'commission', label: '返佣比例/%' },
        { prop: 'assistant_number', label: '助理数量' },
        { prop: 'assistant_commission', label: '助理返佣比例/%' },
        { prop: 'create_time', label: '申请时间' },
      ],
      tableData: [],
      operate: [],
      loading: false,
      showEdit: false,
      showList: false,
      search: {
        province: '',
        city: '',
        county: '',
      },
      countyId: 0,
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 10, // 每页10条数据
      total_nums: 0, // 分组内的总数
    }
  },
  computed: {},
  created() {
    this.loading = true
    // 获取数据
    this._getTableData(this.currentPage, this.pageCount)
    // 操作栏
    this.operate = [
      { name: '审核操作', func: 'handleList', type: 'primary' },
      { name: '编辑', func: 'handleEdit', type: 'primary' },
      { name: '删除', func: 'handleDelete', type: 'danger' },
    ]
  },
  methods: {
    // 获取数据
    async _getTableData(start, count) {
      const res = await partner.getCountyList(start, count, {
        province: this.search.province,
        city: this.search.city,
        county: this.search.county,
      })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
    editClose() {
      this.showEdit = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    listClose() {
      this.showList = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleEdit(val) {
      this.showEdit = true
      this.countyId = Number(val.row.id)
    },
    handleList(val) {
      this.showList = true
      this.countyId = Number(val.row.id)
    },
    handleDelete(val) {
      this
        .$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(async () => {
          this.loading = true
          const res = await partner.deleteCounty(val.row.id)
          if (res.error_code === 0) {
            this.$message.success(`${res.msg}`)
          }
          this._getTableData(this.currentPage, this.pageCount)
        })
    },
    // 搜索省份
    onQueryChange(query) {
      this.search.province = query.trim()
      if (!query) {
        this.currentPage = 1
        this._getTableData(
          this.currentPage,
          this.pageCount,
        )
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索城市
    onQueryChangeOne(query) {
      this.search.city = query.trim()
      if (!query) {
        this.currentPage = 1
        this._getTableData(
          this.currentPage,
          this.pageCount,
        )
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索区/县
    onQueryChangeTwo(query) {
      this.search.county = query.trim()
      if (!query) {
        this.currentPage = 1
        this._getTableData(
          this.currentPage,
          this.pageCount,
        )
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
}
</script>

<style lang="scss" scoped>
    .tableSample {
        padding: 0 0 30px;

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 30px;
            border-bottom: 1px solid #dae1ed;

            .header-left {
                float: left;

                .title {
                    height: 59px;
                    line-height: 59px;
                    color: $parent-title-color;
                    font-size: 16px;
                    font-weight: 500;
                }
            }

            .header-right {
                float: right;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }

        .table-main {
            padding: 0 30px;
            margin-top: 30px;
        }

        .top-operate {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            margin-top: 30px;
        }

        .sort-input {
            width: 50px;
            background: none;
            justify-content: space-around;
        }

        .table-edit {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .content {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .cell-icon {
                cursor: pointer;
                color: #3963bc;
                font-size: 16px;
            }

            .cell-icon-edit {
                display: flex;
                margin-left: 20px;
                width: 50px;
                justify-content: space-between;

                .cell-cancel {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                }

                .cell-save {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                    margin-right: -20px;
                }
            }
        }

        .m-20 {
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .summary {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;

            img {
                width: 135px;
                height: 200px;
            }

            .demo-table-expand {
                font-size: 0;
                margin-left: 30px;
                display: flex;
                flex-direction: column;

                label {
                    width: 90px;
                    color: #99a9bf;
                }

                .el-form-item {
                    margin-right: 0;
                    margin-bottom: 0;
                    display: flex;
                    flex-direction: row;
                }
            }
        }

        .pagination {
            display: flex;
            justify-content: flex-end;
            margin: 20px;
        }
    }
    // dialog
    .tableSample /deep/ .el-dialog__footer {
        text-align: left;
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__header {
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__body {
        padding: 30px;
    }
</style>
